﻿using System;

namespace FlyingChessGame {
    class Program {

        //声明静态字段模拟全局变量
        static int[] Maps = new int[100];
        //声明一个静态数组用来存放玩家A与玩家B的坐标
        static int[] PlayerPos = new int[2];
        //存储两个玩家的姓名
        static string[] PlayerName = new string[2];

        //两个玩家的标记，用来处理暂停
        static bool[] Flags = new bool[2];
        
        static void Main(string[] args) {
            GameShow();
            Console.WriteLine("请输入玩家A的姓名：");
            PlayerName[0] = Console.ReadLine();
            while (PlayerName[0] == " ") {
                Console.WriteLine(" 玩家A的姓名不能为空，请重新输入");
                PlayerName[0] = Console.ReadLine();
            }

            Console.WriteLine("请输入玩家B的姓名：");
            PlayerName[1] = Console.ReadLine();
            while (PlayerName[1] == " "||PlayerName[1]==PlayerName[0]) {
                if (PlayerName[1] == " ") {
                    Console.WriteLine(" 玩家B的姓名不能为空，请重新输入");
                    PlayerName[1] = Console.ReadLine();
                }
                else {
                    Console.WriteLine(" 玩家B的姓名不能和玩家A的姓名重复，请重新输入");
                    PlayerName[1] = Console.ReadLine();
                }
            }
            //玩家姓名输入无误后，首先应该进行清屏
            Console.Clear();
            GameShow();
            Console.WriteLine("玩家{0}用符号A进行标记",PlayerName[0]);
            Console.WriteLine("玩家{0}用符号B进行标记", PlayerName[1]);
            
            InitialMap();
            DrawMap();

            //当玩家A和玩家B没有到终点的时候，游戏继续
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99) {
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else {
                    Flags[0] = false;
                }
                if (PlayerPos[0] >= 99) {
                    Console.WriteLine("玩家{0}取得了胜利！！", PlayerName[0]);
                    Console.Clear();
                    win();

                }
                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else {
                    Flags[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {
                    Console.WriteLine("玩家{0}取得了胜利！！", PlayerName[1]);
                    Console.Clear();
                    win();
                }
            }

            Console.ReadKey();
        }

        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow() {
            Console.ForegroundColor = ConsoleColor.Cyan;//设置前景颜色
            Console.WriteLine("**********************************");
            Console.WriteLine("**********************************");
            Console.WriteLine("**********************************");
            Console.WriteLine("************飞行棋游戏************");
            Console.WriteLine("**********************************");
            Console.WriteLine("**********************************");
            Console.WriteLine("**********************************");
        }


        public static void win() {
            Console.ForegroundColor = ConsoleColor.Red;//设置前景颜色
            Console.WriteLine("                                        ");
            Console.WriteLine("                      ■                 ■■■  ");
            Console.WriteLine("        ■■■■  ■  ■            ■■  ■               ■  ");
            Console.WriteLine("        ■    ■  ■  ■                  ■           ■  ■");
            Console.WriteLine("        ■    ■ ■■■■■■       ■■■■■■■     ■  ■");
            Console.WriteLine("        ■■■■ ■   ■               ■ ■ ■        ■  ■");
            Console.WriteLine("        ■    ■      ■             ■   ■  ■       ■  ■");
            Console.WriteLine("        ■    ■ ■■■■■■      ■     ■   ■      ■  ■");
            Console.WriteLine("        ■■■■      ■          ■      ■     ■    ■  ■");
            Console.WriteLine("        ■    ■      ■                  ■               ■");
            Console.WriteLine("        ■    ■      ■                  ■               ■");
            Console.WriteLine("       ■     ■      ■                  ■               ■");
            Console.WriteLine("     ■    ■■ ■■■■■■■            ■             ■■​ ");

        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitialMap() {

            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
            for (int i = 0;i < luckyTurn.Length;i++) {
                int index = luckyTurn[i];
                Maps[index] = 1;
            }
            int[] lanMine = { 5,13,17,33,38,50,64,80,94};//地雷◆
            for (int i = 0;i < lanMine.Length;i++) {
                int index = lanMine[i];
                Maps[index] = 2;
            }
            int[] pause = { 9,27,60,93};//暂停▲
            for (int i = 0;i < pause.Length;i++) {
                int index = pause[i];
                Maps[index] = 3;
            }
            int[] timeTunel = { 20,25,45,63,72,88,90};//时空隧道※
            for (int i = 0;i < timeTunel.Length;i++) {
                int index = timeTunel[i];
                Maps[index] = 4;
            }

        }

        /// <summary>
        /// 获取坐标对应的标记
        /// </summary>
        /// <param name="i">地图坐标</param>
        /// <returns></returns>
        public static string DrawStringMap(int i) {
            string str = "";
            //如果玩家A的坐标和玩家B的坐标相同，画一个尖括号
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i) {
                str="<>";
            }
            else if (PlayerPos[0] == i) {
                str="Ａ";

            }
            else if (PlayerPos[1] == i) {
                str="Ｂ";
            }
            else {
                switch (Maps[i]) {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str="□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str="◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        str="◆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str="▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.White;
                        str="※";
                        break;
                }//switch
            }//else
            return str;
        }

        /// <summary>
        /// 画地图
        /// </summary>
        public static void DrawMap() {
            Console.WriteLine("图例：幸运轮盘◎   地雷◆   暂停▲   时空隧道※");
            #region 第一横行
            for (int i = 0;i < 30;i++) {
               Console.Write( DrawStringMap(i));
            }//for
            #endregion
            Console.WriteLine();//换行
            #region 第一竖行
            for (int i = 30;i <35;i++) {
                for (int j = 0;j <=28;j++) {
                    Console.Write("  ");
                }
                Console.Write( DrawStringMap(i));
                Console.WriteLine();
            }
            #endregion
            #region 第二横行
            for (int i = 64;i >=35;i--) {
                Console.Write(DrawStringMap(i));
            }
            #endregion
            Console.WriteLine();
            #region 第二竖行
            for (int i = 65;i <= 69;i++) {
                Console.WriteLine(DrawStringMap(i));
                //Console.WriteLine();
            }
            #endregion
            #region 第三横行
            for (int i = 70;i <= 99;i++) {
                Console.Write(DrawStringMap(i));
            }//for
            #endregion
            Console.WriteLine();//换行
        }

        /// <summary>
        /// 玩游戏
        /// </summary>
        public static void PlayGame(int playerNumber) {
            Random r = new Random();
            int rNumber = r.Next(1, 7);

            Console.WriteLine("{0}按下任意键开始掷骰子", PlayerName[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", PlayerName[playerNumber],rNumber);
            PlayerPos[playerNumber] += rNumber;
            ChangePos();
            Console.ReadKey(true);
            Console.WriteLine("{0}按下任意键开始行动", PlayerName[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完了", PlayerName[playerNumber]);
            Console.ReadKey(true);
            //玩家A踩到玩家B
            if (PlayerPos[playerNumber] == PlayerPos[1- playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1}，玩家{1}退6格", PlayerName[playerNumber], PlayerName[1- playerNumber]);
                Console.ReadKey(true);
            }
            else
            {
                switch (Maps[PlayerPos[playerNumber]])
                {
                    case 0:
                        Console.WriteLine("玩家{0}踩到了方块，安全", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("玩家{0}踩到了幸运轮盘，请选择 1--交换位置  2--轰炸对方，使对方退6格", PlayerName[playerNumber]);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}选择和玩家{1}交换位置", PlayerName[playerNumber], PlayerName[1- playerNumber]);
                                Console.ReadKey(true);
                                int temp = PlayerPos[playerNumber];
                                PlayerPos[playerNumber] = PlayerPos[1- playerNumber];
                                ChangePos();
                                PlayerPos[1- playerNumber] = temp;
                                ChangePos();
                                Console.WriteLine("交换完成！玩家{0}按任意键继续", PlayerName[playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{1}退6格", PlayerName[playerNumber], PlayerName[1- playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1- playerNumber] -= 6;
                                ChangePos();
                                Console.WriteLine("玩家{0}退6格完成！", PlayerName[1- playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("请重新输入，只能输入0或1，1--交换位置  2--轰炸对方");
                                input = Console.ReadLine();
                            }
                        }
                        Console.ReadKey(true);
                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷，玩家{0}退6格", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] -= 6;
                        ChangePos();
                        Console.WriteLine("玩家{0}退6格完成！", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停，暂停一轮", PlayerName[playerNumber]);
                        Flags[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道，前进10格", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] += 10;
                        ChangePos();
                        Console.WriteLine("玩家{0}前进10格完成！", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        break;
                }//switch
                Console.Clear();
                DrawMap();
            }//else
        }

        /// <summary>
        /// 当玩家坐标发生改变的时候调用
        /// </summary>
        public static void ChangePos() {
            if (PlayerPos[0] < 0)
            {
                PlayerPos[0] = 0;
            }
            else if (PlayerPos[0] >=99) {
                PlayerPos[0] = 99;
            }

            if (PlayerPos[1] < 0)
            {
                PlayerPos[1] = 0;
            }
            else if (PlayerPos[1] >= 99)
            {
                PlayerPos[1] = 99;
            }
        }


    }
}
